/** 筛选条件
 * @param {Array} list 条件值
 * @param {Array} arr 数据值
 */
export const filtratePact = (lists = [], array = []) => {
  return lists.filter(list => {
    const { conditionType, customerFormConditionDetailViews } = list;
    if (conditionType == 10) {
      return customerFormConditionDetailViews.some(item => judgeCondition(item, array));
    } else if (conditionType == 20) {
      return customerFormConditionDetailViews.every(item => judgeCondition(item, array));
    } else if (conditionType == 0) return true;
    else return false;
  });
};

/**
 * 判断单个条件
 * @param {Object} item 条件
 * @param {Array} array 值数组
 * @returns
 */
function judgeCondition(item, array) {
  const { condition, value = "", formTitle } = item;
  const o = array.find(arr => arr.title == formTitle);
  if (!o) return false;
  return convertFunc(o, condition, value);
}

/**
 * 转换function
 * @param {Object} o 目标值
 * @param {String} condition 条件
 * @param {String} value 指定值
 * @returns
 */
function convertFunc(o, condition, value) {
  if (!condition) return false;
  const before = convertStr(o.value);
  const after = convertStr(value);
  switch (condition) {
    case "contain":
      return value.indexOf(o.value) !== -1;
    case "noContain":
      return value.indexOf(o.value) == -1;
    case ">":
      return before > after;
    case "<":
      return before < after;
    case ">=":
      return before >= after;
    case "<=":
      return before <= after;
    case "==":
      return before == after;
  }
}

/**
 * 转换值
 * @param {String} val 值
 * @returns
 */
function convertStr(val) {
  return val;
}
